﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CH.Easy.DbProvider.Core;
using System.Linq.Expressions;
using CH.Easy.DbProvider.Common;
using CH.Easy.DbProvider.Entity;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Reflection;
using CH.Easy.Utility;
namespace CH.Easy.DbProvider.Provider
{
    public class MsSql2005DbProvider : DbTableProvider
    {
      
        public override DbConnection DbConnection
        {
            get
            {
                return new SqlConnection(ConfigHelper.GetTableConnectionString(base.TableName));
            }
        }


        public override DbProviderType DbProviderType
        {
            get { return DbProviderType.MsSql2005; }
        }


      


        public override object ExecuteReader(SolutionResult solutionResult)
        {
            SqlDataReader dr = DbSqlHelper.ExecuteReader((SqlConnection)this.DbConnection, CommandType.Text, solutionResult.CmdText, solutionResult.Parameter.Cast<SqlParameter>().ToArray());
            solutionResult.ResultExpression = this.MappingResultExpression(solutionResult.ResultExpression, dr);
            if (solutionResult.IsPage) 
            {
                int totalRecord = 0;
                object result = DbSqlHelper.ExecuteScalar((SqlConnection)this.DbConnection, CommandType.Text, solutionResult.CountSolutionResult.CmdText, solutionResult.CountSolutionResult.Parameter.Cast<SqlParameter>().ToArray());
                if (result != null) 
                {
                    totalRecord = (int)result;
                }
                var dbQueryEnumerable = Activator.CreateInstance(typeof(DbQueryEnumerable<>).MakeGenericType(solutionResult.ResultType), BindingFlags.Instance | BindingFlags.Public, null, new object[] { dr, solutionResult.ResultExpression }, null);

                return Activator.CreateInstance(typeof(QueryResult<>).MakeGenericType(solutionResult.ResultType), new object[] { dbQueryEnumerable, solutionResult.PageIndex, solutionResult.PageSize, totalRecord });
            }
            
            
            return Activator.CreateInstance(typeof(DbQueryEnumerable<>).MakeGenericType(solutionResult.ResultType), BindingFlags.Instance | BindingFlags.Public, null, new object[] { dr, solutionResult.ResultExpression }, null);
        }

        public override object ExecuteNonQuery(SolutionResult solutionResult)
        {
            object result = DbSqlHelper.ExecuteNonQuery((SqlConnection)this.DbConnection, CommandType.Text, solutionResult.CmdText, solutionResult.Parameter.Cast<SqlParameter>().ToArray());
            return result;
        }

        public override object ExecuteScalar(SolutionResult solutionResult)
        {
            object result = DbSqlHelper.ExecuteScalar((SqlConnection)this.DbConnection, CommandType.Text, solutionResult.CmdText, solutionResult.Parameter.Cast<SqlParameter>().ToArray());
            return result;
        }

        public override object ExecuteCommand(string cmdText, CommandType commandType, ExecuteType executeType, params DbParameter[] parameter)
        {
            return typeof(DbSqlHelper).GetMethod(executeType.ToString(),new Type[]{ typeof(SqlConnection), typeof(CommandType), typeof(string), typeof(SqlParameter[])}).Invoke(this, new object[] { (SqlConnection)this.DbConnection, commandType, cmdText, parameter.Cast<SqlParameter>().ToArray() });
        }

    }
}
